import { message } from 'antd';
import {
  getEmpList,
  getReplaceTask,
  getPersonData,
  createTask,
  excutedTask,
} from '@/services/replacemanage';

export default {
  state: {
    activeKey: '1',
    formFieldsValue: {}, // 表单域
    expandForm: false, // 展开收起
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    valuesArr: ['taskType', 'areaId', 'operTime'],
    tabList: [{ name: '已创建' }, { name: '待执行' }, { name: '执行中' }, { name: '已完成' }],
    replTask: {}, // 替数任务详情上面数据
    replReqs: [], // 涉及替数请求 （替数任务详情中间数据）
    pageTaskData: [], // 替数明细列表数据
    pagePersonData: {
      // 人员明细列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    perDetailInfo: '',
  },
  effects: {
    // 获取替数任务列表数据
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取替数任务详情
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(getReplaceTask, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          replTask: {},
          replReqs: [],
          pageTaskData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              replTask: response.data.replTask,
              replReqs: response.data.replReqs,
              pageTaskData: response.data.replTaskDetVos,
            },
          });
        }
      } else {
        message.warning(`${response.message || '获取替数任务详情：服务端错误！'}`);
      }
    },
    // 获取人员明细表格数据
    *fetchPersonList({ payload }, { call, put }) {
      const response = yield call(getPersonData, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getPersonList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
             perDetailInfo: response.data.val || '',
            },
          });
          yield put({
            type: 'getPersonList',
            payload: {
              data: response.data.pageRTEV,
              currentPage,
              pageSize,
            },
          });
        }
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 生成替数明细，执行替数
    *creExcuteTask(
      {
        payload: { type, params },
      },
      { call }
    ) {
      const response = yield call(type === 1 ? createTask : excutedTask, params);
      return response;
    },
  },
  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取人员明细列表
    getPersonList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pagePersonData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
